package com.lichhao.common.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.data.mongodb.core.query.Order;

public class QueryParam {

	private String page; // Current page number.
	private String sortname; // The name of the column to sort by.
	private String sortorder; // The order to sort by – ‘asc’ or ‘desc’.
	private String qtype; // The column selected during ‘quick search’.
	private String query; // The text used within a search.
	private String rp; // The number of records to be returned.

	public int getSkip() {
		return (this.getPageNumber() - 1) * this.getLimit();
	}

	public int getPageNumber() {
		if (NumberUtils.isDigits(page)) {
			return Integer.parseInt(page);
		}
		return 1;
	}

	public int getLimit() {
		if (NumberUtils.isNumber(rp)) {
			return Integer.parseInt(rp);
		}
		return Integer.MAX_VALUE;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getSortname() {
		return sortname;
	}

	public void setSortname(String sortname) {
		this.sortname = sortname;
	}

	public Order getSortorder() {

		if (this.sortorder.equalsIgnoreCase("asc")) {
			return Order.ASCENDING;
		}
		if (this.sortorder.equalsIgnoreCase("desc")) {
			return Order.DESCENDING;
		}

		return Order.ASCENDING;
	}

	public String getQtype() {
		return qtype;
	}

	public void setQtype(String qtype) {
		this.qtype = qtype;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public String getRp() {
		return rp;
	}

	public void setRp(String rp) {
		this.rp = rp;
	}

	public QueryParam(HttpServletRequest request) {
		this.page = request.getParameter("page");
		this.sortname = request.getParameter("sortname");
		this.sortorder = request.getParameter("sortorder");
		this.qtype = request.getParameter("qtype");
		this.query = request.getParameter("query");
		this.rp = request.getParameter("rp");

	}

}
